#pragma once
#include<iostream>
#include<unordered_map>
#include<fstream>
#include<string>
#include"log.hpp"

using namespace LogModule;
std::string default_dict_path="./dictionary.txt";
std::string sep=":";
class dict
{
    public:
    dict(const std::string dict=default_dict_path)
    :_dict_path(dict)
    {}
    //字典的加载
    void Loaddict()
    {
        //这里需要的是输入流
        std::ifstream in(_dict_path);
        std::string line;
        while(std::getline(in,line))
        {
            //处理字符串
            auto pos=line.find(sep);
            if(pos==std::string::npos)
            {
                LOG(LogLevel::ERROR)<<"字典格式错误！";
                //继续读
                continue;
            }
            std::string english=line.substr(0,pos);
            std::string chinese=line.substr(pos+sep.size());
            if(english.empty()||chinese.empty())
            {
                LOG(LogLevel::ERROR)<<"字典格式错误！";
                continue;
            }
            _dict.insert(std::make_pair(english,chinese));
            std::cout<<"加载 "<<line<<" 成功！"<<std::endl;
        }

        in.close();
    }
    //提供翻译功能
    std::string Translate(std::string& english)
    {
        auto iter=_dict.find(english);
        if(iter==_dict.end())
        {
            LOG(LogLevel::INFO)<<"没有找到";
            return "None";
        }
        return iter->second;
    }
    ~dict()
    {}
    private:
    std::string _dict_path;
    std::unordered_map<std::string,std::string> _dict;
};